var express = require('express');
var router = express.Router();
var { prepayModel, collectModel } = require("../module/prepay")

// 预付款列表
router.get('/list', async (req, res) => {
    let relations = req.query.relation
    let relation = new RegExp(relations, 'i')
    let usernames = req.query.username
    let username = new RegExp(usernames, 'i')
    let banks = req.query.bank
    let bank = new RegExp(banks, 'i')
    let genders = req.query.gender
    let gender = new RegExp(genders, 'i')
    let handles = req.query.handle
    let handle = new RegExp(handles, 'i')
    let query = { $and: [] }
    if (relations != '' && relations != 'undefined') {
        query.$and.push({ relation: relation })
    }
    if (usernames != '' && usernames != 'undefined') {
        query.$and.push({ username: username })
    }
    if (banks != '' && banks != 'undefined') {
        query.$and.push({ bank: bank })
    }
    if (genders != '' && genders != 'undefined') {
        query.$and.push({ gender: gender })
    }
    if (handles != '' && handles != 'undefined') {
        query.$and.push({ handle: handle })
    }
    let dataList = await prepayModel.find(query).exec()

    res.send({ dataList })
});

router.post('/addaddlist', async (req, res) => {
    let body = req.body
    let dataList = await prepayModel.create(body)
    // console.log(dataList);
    res.send({ dataList, msg: '添加成功', code: 200 },)
});
router.patch('/editaddlist', async (req, res) => {
    let _id = req.body._id
    let body = req.body
    let dataList = await prepayModel.updateOne({ _id: _id }, { $set: body })
    // console.log(dataList);
    res.send({ dataList, msg: '修改成功', code: 200 },)
});

router.delete('/del/:id', async (req, res) => {
    let _id = req.params.id
    await prepayModel.deleteOne({ _id: _id })
    res.send({ code: 200, msg: '删除成功' })
})

// 期初应收列表
router.get('/list', async (req, res) => {
    let dataList = await collectModel.find()
    // console.log(dataList);
    res.send({ dataList, code: 200, msg: '查询成功' })
});
module.exports = router;